<?php
/**
 * User: Allen
 * Date: 15-11-25
 * 文章控制器
 */

class Article extends PC_Controller{
	
	protected $go_url = '/admin/article/article_list';
	public function __construct(){
		parent::__construct();
		$this->load->model('article_model');
		$this->load->model('type_model');
	}
	
	/**
	 * @brief 文章信息页面
	 * @param Null
	 * @par 2015/11/25 Ver 1.00 Created by Allen
	 */	
	public function article_list() {
		$message = 1;
		if($this->uri->segment(4)) {
			$message = $this->uri->segment(4);
		}
		$this->data['message'] = $message;
		$this->data['title'] =  empty($_POST) ? '' : $_POST['title'];
		$this->data['tid'] =  empty($_POST) ? '' : $_POST['t_id'];
		$type_list = $this->type_model->getTypeAll();
		$this->data['type_list'] = $type_list;
		//判断权限
		$this->data['authority'] = $this->authority;
		$this->load->view('/admin/article_list.php',$this->data);
	}
	
	/**
	 * @brief ajxa获取文章信息
	 * @param Null
	 * @par 2015/11/25 Ver 1.00 Created by Allen
	 */
	public function ajax_article_list() {
		$data = $_GET;
		$start = $data['iDisplayStart']; //显示的起始索引
		$length = $data['iDisplayLength'];//显示的行数
		$sort_th = $data['mDataProp_'.$data['iSortCol_0'].''];//被排序的列 
		$sort_type = $data['sSortDir_0'];//排序的方向 "desc" 或者 "asc".
		$search = $data ['sSearch'];//全局搜索字段
		$data['title'] ? $where['article.title like'] = '%'.trim($data['title']).'%' :'';
		if($data['t_id']) {
			$where['article.t_id'] = trim($data['t_id']);			
		}
		$where['concat(article.id) like'] = '%'.trim($search).'%';
		$where['t_id >'] = 1; //排除信息的分类
		$this->data['count'] = $this->article_model->getCount($where);
		$aaData = array();
		if($this->data['count']) {
			$order_by = $sort_th." ".$sort_type;
			$article_list = $this->article_model->getArticleList($where,$length,$start,$order_by);
			foreach($article_list as $key => $value) {
				if(!$value['litpic']) {
					$value['litpic'] = '/public_source/www/images/default.jpg';
				}
				$article_list[$key]['check'] = '<label><input name="c_id" type="checkbox" value="'.$value['id'].'" /></label>';
				$article_list[$key]['publish_time'] = date("Y-m-d H:i:s",$value['publish_time']);
				$article_list[$key]['litpic'] = '<img src="'.$value['litpic'].'" style="max-height:30px;"/>';
				$authority = $this->authority;
				$edit_url = '';
				if($authority['edit_status'] == 2) {
					$edit_url = '<a href="/admin/article/edit_page/'.$value['id'].'"><span class="btn btn-xs btn-primary">编辑</span></a>';
				}
				$del_url = '';
				if($authority['del_status'] == 2) {
					$del_url = '<a onclick="deleteOne(this)"  data_id = '.$value['id'].'><span class="btn btn-xs btn-pink">删除</span></a>';
				}
				$article_list[$key]['operate'] = $edit_url." ".$del_url;
				if(empty($del_url) && empty($edit_url)) {$article_list[$key]['operate'] = '无';};
			}
			$aaData = $article_list;
		}
		$output['aaData'] = $aaData;
		$output['sEcho'] = $_GET['sEcho'];
		$output['iTotalDisplayRecords'] =  $this->data['count']; //总共有几条数据
		$output['iTotalRecords'] = $this->data['count']; //总共有几条数据
		echo json_encode($output); //最后把数据以json格式返回
	}
	
	/**
	 * @brief 编辑页面
	 * @param Null
	 * @par 2015/11/26 Ver 1.00 Created by Allen
	 */
	public function edit_page() {
		$id = $this->uri->segment(4);
		$where['id'] = $id;
		$article_info = $this->article_model->checkArticle($where);
		$type_list = $this->type_model->getTypeAll();
		$this->data['data'] = $article_info;
		if($article_info['litpic']) {
			$this->data['pic'] = $article_info['litpic'];
		}else {
			$this->data['pic'] = '/public_source/www/images/default.jpg';
		}
		$this->data['type_list'] = $type_list;
		$this->load->view('/admin/article_edit',$this->data);
	}
			
	/**
	 * @brief 保存编辑信息
	 * @param Null
	 * @par 2015/11/26 Ver 1.00 Created by Allen
	 */
	public function edit() {
		$data = $_POST;
		$litpic = $data['litpic'];
		if(is_uploaded_file($_FILES['jietu']['tmp_name'])) {
			$litpic = $this->upload($_FILES);
		}
		$data['litpic'] = $litpic;
		$data['publish_time'] = strtotime($data['publish_time']);
		$data['update_time'] = time();
		$where['id'] = $data['id'];
		$edit_result = $this->article_model->editArticle($where,$data);
		if($edit_result) {
			$this->location_href($this->go_url."/2");	
		}else {
			$this->location_href($this->go_url."/3");	
		}
	}
	
	/**
	 * @brief 删除文章信息
	 * @param Null
	 * @par 2015/11/25 Ver 1.00 Created by Allen
	 */
	public function delete() {
		$data = $_POST;
		$id = $_POST['id'];
		$where['id'] = $id;
		$del_result = $this->article_model->deleteArticle($where);
		echo json_encode($del_result);
	}
	
	/**
	 * @brief 删除多行文章信息
	 * @param Null
	 * @par 2015/11/25 Ver 1.00 Created by Allen
	 */
	public function delete_all() {
		$data = $_POST;
		$id_list = array_filter(explode(',',$data['str']));
		//$id_list = array(3,4);
		$del_result = $this->article_model->deleteAll($id_list);
		echo json_encode($del_result);
	}

	/**
	 * @brief 添加页面
	 * @param Null
	 * @par 2015/11/26 Ver 1.00 Created by Allen
	 */
	public function add_page() {
		$type_list = $this->type_model->getTypeAll();
		$this->data['type_list'] = $type_list;
		$this->data['now_time'] = date("Y-m-d H:i:s",time());
		$this->load->view('/admin/article_add',$this->data);
	}
	
	/**
	 * @brief 保存添加信息
	 * @param Null
	 * @par 2015/11/26 Ver 1.00 Created by Allen
	 */
	public function add() {
		$data = $_POST;
		$litpic = $data['litpic'];
		if(is_uploaded_file($_FILES['jietu']['tmp_name'])) {
			$litpic = $this->upload($_FILES);
		}
		$data['litpic'] = $litpic;
		$data['publish_time'] = strtotime($data['publish_time']);
		$data['create_time'] = $data['update_time'] = time();
		$add_result = $this->article_model->addArticle($data);
		if($add_result) {
			$this->location_href($this->go_url.'/4');
		}else {
			$this->location_href($this->go_url.'/5');
		}
	}
}
?>